<template>
    <div>
        <van-tabs v-model="active">
            <van-tab title="已过期物品"></van-tab>
        </van-tabs>
        <van-card v-for="(item, index) in idarr" :key="index" v-if="timearr[index] != -1" :num="timearr[index]" tag="已过期"
            :title="namearr[index]" :thumb="imgarr[index]" origin-price="建议使用" />
    </div>

</template>

<script >
const AV = require('leancloud-storage');
const { Query, User } = AV;
export default {
    data() {
        return {
            active: 0,
            namearr: [],
            timearr: [],
            idarr: [],
            typearr: [],
            dayarr: [],
            imgarr: []
        }
    },
    methods: {
        goToItemPage(id) {
            this.$router.push({
                path: "/item",
                query: {
                    id: id
                }
            })
        },

        getTime(date) {
            let y = date.getFullYear() //年
            let m = date.getMonth() + 1  //月，月是从0开始的所以+1
            let d = date.getDate() //日
            m = m < 10 ? '0' + m : m //小于10补0
            d = d < 10 ? '0' + d : d //小于10补0
            return (y + '-' + m + '-' + d); //返回时间形式yyyy-mm-dd
        },
        
        getDaysBetween(date1, date2) {
            var startDate = Date.parse(date1);
            var endDate = Date.parse(date2);
            if (startDate >= endDate) {
                return (startDate - endDate) / (1 * 24 * 60 * 60 * 1000);
            }
            return -1;
        }
    },
    created: function () {

        const query = new AV.Query('FoodPeriod');
        // query.equalTo('Item', '香蕉');
        query.find().then((students) => {
            students.forEach((itemson) => {
                let data = itemson.attributes;
                let imgSrc;
                if (data.img == null) {
                    imgSrc = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fforum.nos-jd.163yun.com%2Fforum%2Fwh%2Fforum%2F201910%2F31%2F202211c6me4xwe04s9wpep.gif&refer=http%3A%2F%2Fforum.nos-jd.163yun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659282694&t=0d6fe8062dd2645f3a24d425e89311e3";
                } else {
                    imgSrc = data.img.attributes.url;
                }
                const ID = itemson.id;
                const Type = itemson.get('Type');
                const name = itemson.get('Item');//名字
                const timeStart = itemson.get('Start');//开始日期
                const timeEnd = itemson.get('Deadline');//结束日期
                const day = itemson.get('ShelfLife');//保质期
                const NumberOfDays = Math.ceil(this.getDaysBetween(this.getTime(new Date), timeEnd));//相差几天
                this.namearr.push(name);
                this.timearr.push(NumberOfDays);
                this.idarr.push(ID);
                this.typearr.push(Type);
                this.dayarr.push(day);
                this.imgarr.push(imgSrc);
            });
        });


    },
}


</script>

<style >
</style>